#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "kgroupbutton.h"
#include <QHBoxLayout>
#include "groupbuttonproxystyle.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(420, 240);
    QVBoxLayout *mainLayout = new QVBoxLayout();
    KGroupButton *groupBtn1 = new KGroupButton();
    groupBtn1->setText("GroupBtn1");
    groupBtn1->setCheckable(true);
    groupBtn1->setAutoExclusive(true);
    groupBtn1->setChecked(true);
    groupBtn1->setPosition(GroupButtonStyleOption::Begin);
    KGroupButton *groupBtn2 = new KGroupButton();
    groupBtn2->setText("GroupBtn2");
    groupBtn2->setCheckable(true);
    groupBtn2->setAutoExclusive(true);
    groupBtn2->setPosition(GroupButtonStyleOption::Middle);
    KGroupButton *groupBtn3 = new KGroupButton();
    groupBtn3->setText("GroupBtn3");
    groupBtn3->setCheckable(true);
    groupBtn3->setAutoExclusive(true);
    groupBtn3->setPosition(GroupButtonStyleOption::End);

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->setSpacing(1);
    btnLayout->addWidget(groupBtn1);
    btnLayout->addWidget(groupBtn2);
    btnLayout->addWidget(groupBtn3);

    mainLayout->addLayout(btnLayout);
    ui->centralwidget->setLayout(mainLayout);

    if (QGSettings::isSchemaInstalled("org.ukui.style")) {
        styleSettings = new QGSettings("org.ukui.style");
        connect(styleSettings, &QGSettings::changed, this, [=](const QString &key) {
            if (key == "styleName") {
                qDebug()<<"style name changed";

                auto styleName = styleSettings->get("styleName").toString();

                if (styleName == "ukui-default" || styleName == "ukui-dark" || styleName == "ukui-white"
                        || styleName == "ukui-black" || styleName == "ukui-light" || styleName == "ukui") {
                    if (styleName == "ukui")
                        styleName = "ukui-default";
                    else if (styleName == "ukui-black")
                        styleName = "ukui-dark";
                    else if (styleName == "ukui-white")
                        styleName = "ukui-light";

                    qApp->setStyle(new GroupButtonProxyStyle(styleName));
                    return;
                }
            }
        });
        auto styleName = styleSettings->get("styleName").toString();
        qApp->setStyle(new GroupButtonProxyStyle(styleName));
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

